change get_origin_path_from_id by sqlite

Brightcells 8 jaren geleden
bovenliggende
commit
9d9a1c7461
1 gewijzigde bestanden met toevoegingen van 32 en 26 verwijderingen
  1. 32 26
      main.py

+ 32 - 26
main.py

@@ -24,22 +24,27 @@ CREATE_TABLE_STMT = """
24 24
     id integer primary key,
25 25
     lensman varchar(20),
26 26
     session varchar(20),
27
-    name varchar(13)
27
+    photo_id varchar(13),
28
+    photo_name varchar(255),
29
+    thumb_path varchar(255),
30
+    origin_path varchar(255)
28 31
 );"""
29 32
 # Create Index SQL
30 33
 CREATE_INDEX1 = 'CREATE INDEX IF NOT EXISTS idx_lensman ON photoinfo (lensman);'
31 34
 CREATE_INDEX2 = 'CREATE INDEX IF NOT EXISTS idx_session ON photoinfo (session);'
32 35
 # Insert Record SQL
33
-INSERT_RECORD_STMT = 'INSERT INTO photoinfo VALUES (NULL, ?, ?, ?);'
36
+INSERT_RECORD_STMT = 'INSERT INTO photoinfo VALUES (NULL, ?, ?, ?, ?, ?, ?);'
34 37
 # Delete Record SQL
35 38
 DELETE_RECORD_STMT = 'DELETE FROM photoinfo WHERE lensman = ? and session = ? and name = ?;'
36
-# Query Max(name) SQL
37
-SELECT_RECORD_STMT = 'SELECT MAX(name) FROM photoinfo WHERE lensman = ? and session = ?;'
39
+# Query Max(photo_id) SQL
40
+SELECT_MAX_PHOTO_ID_STMT = 'SELECT MAX(photo_id) FROM photoinfo WHERE lensman = ? and session = ?;'
41
+# Query Origin Path SQL
42
+SELECT_ORIGIN_PATH_STMT = 'SELECT origin_path FROM photoinfo WHERE lensman = ? and photo_id = ?;'
38 43
 
39 44
 
40 45
 conn = sqlite3.connect('minipai2.db')
41 46
 cur = conn.cursor()
42
-# 执行语句
47
+# Execute SQL
43 48
 cur.execute(CREATE_TABLE_STMT)
44 49
 cur.execute(CREATE_INDEX1)
45 50
 cur.execute(CREATE_INDEX2)
@@ -81,13 +86,13 @@ def create_session_dir(lensman, session):
81 86
 
82 87
 
83 88
 def get_last_timestamp(lensman, session):
84
-    cur.execute(SELECT_RECORD_STMT, (lensman, session))
89
+    cur.execute(SELECT_MAX_PHOTO_ID_STMT, (lensman, session))
85 90
     result = cur.fetchall()
86 91
     return int(result[0][0] or 0)
87 92
 
88 93
 
89
-def insert_session_file(lensman, session, name):
90
-    cur.execute(INSERT_RECORD_STMT, (lensman, session, name))
94
+def insert_session_file(lensman, session, id, name, thumb, origin):
95
+    cur.execute(INSERT_RECORD_STMT, (lensman, session, id, name, thumb, origin))
91 96
     conn.commit()
92 97
 
93 98
 
@@ -100,30 +105,31 @@ def get_new_files(lensman, session, maxt):
100 105
     _, thumb = get_session_dir(lensman, session)
101 106
     files = glob.iglob('{}/*'.format(thumb))
102 107
     news = []
103
-    for file in files:
104
-        if not file.endswith('.tmp'):  # Whether 'xxx.tmp' or not
105
-            filename = file.split('/')[-1]
106
-            name = filename.split('.')[0]
107
-            if int(name) > maxt:
108
-                insert_session_file(lensman, session, name)
108
+    for file_ in files:
109
+        if not file_.endswith('.tmp'):  # Whether 'xxx.tmp' or not
110
+            photo_name = file_.split('/')[-1]
111
+            photo_id = photo_name.split('.')[0]
112
+            thumb_path = file_.strip(ROOT_PATH)
113
+            origin_path = thumb_path.replace('thumbnail', 'origin')
114
+            if int(photo_id) > maxt:
115
+                insert_session_file(lensman, session, photo_id, photo_name, thumb_path, origin_path)
109 116
                 news.append({
110
-                    'id': name,
111
-                    'name': filename,
112
-                    'path': file.strip(ROOT_PATH),
117
+                    'id': photo_id,
118
+                    'name': photo_name,
119
+                    'path': thumb_path,
113 120
                 })
114 121
     return news
115 122
 
116 123
 
117 124
 def get_origin_path_from_id(lensman, session, id_):
118
-    origin, _ = get_session_dir(lensman, session)
119
-    files = glob.iglob('{}/*'.format(origin))
120
-    for file in files:
121
-        if not file.endswith('.tmp'):  # Whether 'xxx.tmp' or not
122
-            filename = file.split('/')[-1]
123
-            name = filename.split('.')[0]
124
-            if name == id_:
125
-                return file.strip(ROOT_PATH)
126
-    return ''
125
+    cur.execute(SELECT_ORIGIN_PATH_STMT, (lensman, id_))
126
+    result = cur.fetchall()
127
+    if not result:
128
+        return ''
129
+    origin_path = result[0][0]
130
+    if not os.path.isfile('{}/{}'.format(ROOT_PATH, origin_path)):
131
+        return ''
132
+    return origin_path
127 133
 
128 134
 
129 135
 class HelloHandler(RequestHandler):